<?php
/**
 * Created by PhpStorm.
 * User: hanbin
 * Date: 17/4/1
 * Time: 下午1:49
 */

namespace App\Http\Controllers\Zhihu;


use App\Logic\zhihu\MemberLogic;
use App\Logic\zhihu\PublicLogic;
use Validator;

/**
 * <About>公共接口</About>
 *
 * Class PublicController
 * @package App\Http\Controllers\Auth
 */
class PublicController extends BaseController
{

    public function __construct()
    {
        $this->setToken(false);
        parent::__construct();
    }

    /**
     * 注册
     *
     * @return array
     */
    public function register()
    {
        $rmsg=file_get_contents( "php://input");
        $resultcode = json_decode($rmsg,true);
        $rule = [
            'phone' => 'required|string',
            'userpwd' => 'required|string',
            'code' => 'required|string',
        ];
        $message = [
            'phone.regex' => '手机号码有误',
            'userpwd.required' => '密码不能为空',
            'code.required' => '验证码不能为空',
        ];
        $validator = Validator::make($resultcode, $rule, $message);

        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::register($resultcode);
    }

    /**
     * 登录
     *
     * @return array|void
     */
    public function login()
    {
        $rmsg=file_get_contents( "php://input");
        $resultcode = json_decode($rmsg,true);
        $rule = [
            'phone' => 'required|regex:/^1[34578][0-9]{9}$/',
            'userpwd' => 'required|string',
        ];
        $message = [
            'phone.regex' => '手机号码有误',
            'userpwd.required' => '密码不能为空',
        ];
        $validator = Validator::make($resultcode, $rule, $message);

        $errors = $validator->errors()->all();

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::login($resultcode);
    }

    /**
     * 忘记密码
     *
     * @return array|void
     */
    public function forget()
    {
        $rmsg=file_get_contents( "php://input");
        $resultcode = json_decode($rmsg,true);
        $rule = [
            'phone' => 'required|regex:/^1[34578][0-9]{9}$/',
            'userpwd' => 'required|string',
            'code' => 'required|string',
        ];
        $message = [
            'phone.regex' => '手机号码有误',
            'userpwd.required' => '密码不能为空',
            'code.required' => '验证码不能为空',
        ];
        $validator = Validator::make($resultcode, $rule, $message);

        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息

        //如果验证不通过
        if (!empty($errors) && !$validator->passes()) {
            return $this->error($errors[0], 206);
        }
        return MemberLogic::forget($resultcode);
    }

    /**
     * <document>
     * <name>轮播图</name>
     * <author>hanbin</author>
     * <link>http://47.93.190.18/api/zh/public/banner</link>
     * <data></data>
     * <return>返回值|返回值说明|返回值类型,
     * id|地区ID|必须|int,
     * name|省市区名|必须|string</return>
     *</document>
     */
    public function banner()
    {
        return PublicLogic::banner(4);
    }


}